function hfile = prt_ConvertToSingleTrial(hfile)
% PRT::ConvertToSingleTrial  - convert to a single-trial version
%
% FORMAT:       [prt =] prt.ConvertToSingleTrial;
%
% No input fields.
%
% Output fields:
%
%       prt         altered PRT
%
% Examples:
%
%   prt.ConvertToSingleTrial;
%

% Version:  v0.7g
% Build:    9072022
% Date:     Jul-20 2009, 10:58 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'prt')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
if isempty(bc.Cond)
    return;
end

% get number of conditions
ncon = numel(bc.Cond);
too = 0;
for cc = 1:ncon
    too = too + size(bc.Cond(cc).OnOffsets, 1);
end
nc = bc.Cond(1);
nc.NrOfOnOffsets = 1;
nc.Weights = 1;
nc = nc(ones(1, too));
too = 0;
for cc = 1:ncon
    sc = bc.Cond(cc);
    for scc = 1:size(sc.OnOffsets, 1)
        too = too + 1;
        nc(too).ConditionName = {sprintf('%s_%d', sc.ConditionName{1}, scc)};
        nc(too).OnOffsets = sc.OnOffsets(scc, :);
        if numel(sc.Weights) == size(sc.OnOffsets, 1)
            nc(too).Weights = sc.Weights(scc);
        end
        nc(too).Color = sc.Color;
    end
end

% set new content
bc.Cond = nc;
bc.NrOfConditions = numel(nc);
bvqxfile_setcont(hfile.L, bc);
